package com.lixh.sdk.base.literoute

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner

//以后慢慢扩展吧
class IntentWrapper() : Intent() {

    var fragment: Fragment? = null

    inline fun <reified T> get(): T {
        fragment?.arguments = extras;
        return (fragment is T) as T
    }

    fun go(context: Context) {

        component?.let {
            setClassName(context, it.className)
        }
        context.startActivity(this)
    }

    fun goAndFinish(context: Activity) {
        component?.let {
            setClassName(context, it.className)
        }
        context.startActivity(this)
        context.finish()
    }

    fun go(context: Fragment) {
        component?.let {
            setClassName(context.requireContext(), it.className)
        }
        context.startActivity(this)

    }

    fun goAndFinish(context: Fragment) {
        component?.let {
            setClassName(context.requireContext(), it.className)
        }
        context.startActivity(this)
        context.requireActivity().finish()
    }

    fun launch(context: LifecycleOwner, launcher: ActivityResultLauncher<Intent>) {

        when (context) {
            is ComponentActivity -> {
                component?.let {
                    setClassName(context, it.className)
                }
                launcher.launch(this)
            }
            is Fragment -> {
                component?.let {
                    setClassName(context.requireContext(), it.className)
                }
                launcher.launch(this)
            }

        }
    }

}